﻿using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace LetsDoIt.Controls
{
    public partial class PictureBoxEx : UserControl
    {

        #region Transparent Color
        Color _transparentColor;
        /// <summary>
        /// Sets or gets transparent color for an Image. The default color is 255, 0, 255.
        /// </summary>
        public Color TransparentColor
        {
            get
            {
                return _transparentColor;
            }
            set
            {
                _transparentColor = value;
                this.Invalidate();
            }
        }
        #endregion

        public PictureBoxEx()
        {
            InitializeComponent();
            _transparentColor = Color.FromArgb(255, 0, 255);
        }

        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.Clear(Parent.BackColor);
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            if (Image != null)
            {
                ImageAttributes attr = new ImageAttributes();
                attr.SetColorKey(_transparentColor, _transparentColor);
                e.Graphics.DrawImage(this.Image, this.ClientRectangle, 0, 0, this.Image.Width, this.Image.Height, GraphicsUnit.Pixel, attr);
            }
            if(selected)
            {
                Pen selectedPen = new Pen(this.ForeColor, 8);
                Rectangle rect = this.ClientRectangle;
                //rect = new Rectangle(rect.X - 2, rect.Y - 2, rect.Width, rect.Height);
                e.Graphics.DrawRectangle(selectedPen, rect);
            }
        }

        object _lock = new object();
        private Image image;
        public Image Image
        {
            get
            {
                lock (_lock)
                {
                    return image;
                }
            }
            set
            {
                lock (_lock)
                {
                    if (image != null)
                        image.Dispose();

                    image = new Bitmap(this.Width, this.Height);
                    using (Graphics g = Graphics.FromImage(image))
                    {
                        g.Clear(TransparentColor);
                        ImageAttributes attr = new ImageAttributes();
                        attr.SetColorKey(_transparentColor, _transparentColor);
                        g.DrawImage(value, this.ClientRectangle, 0, 0, value.Width, value.Height, GraphicsUnit.Pixel,
                                    attr);
                    }
                }
                //value.Dispose();
                //value = null;
                this.Invalidate();
            }
        }

        private bool selected;
        public bool Selected
        {
            get { return selected; }
            set { selected = value; }
        }

        public new void Dispose()
        {
            image.Dispose();
            image = null;
        }
    }
}